package com.qili.controller.sys;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qili.base.entity.CurrentUser;
import com.qili.core.annotation.Log;
import com.qili.entity.supervisor.SupervisorFile;
import com.qili.entity.supervisor.SupervisorProject;
import com.qili.entity.supervisor.dto.ProjectBigEventDTO;
import com.qili.entity.supervisor.dto.SupervisorFileDTO;
import com.qili.entity.supervisor.dto.SupervisorProjectDTO;
import com.qili.entity.sys.SysUser;
import com.qili.exception.MyException;
import com.qili.service.supervisor.SupervisorFileService;
import com.qili.util.*;
import io.swagger.annotations.ApiOperation;
import net.bytebuddy.asm.Advice;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Date;
import java.util.List;

/**
 * @author zhaoyongke
 * @date 2021/8/10
 * @description  签章管理
 */
@Controller
@RequestMapping("/sys/sealManager")
public class SealController {


    @Autowired
    SupervisorFileService  supervisorFileService;

    @Autowired
    UploadUtil uploadUtil;

    @GetMapping(value = "index")
    public String index(HttpServletRequest request) {
        return "/system/seal/seallist";
    }

    /**
    * @Author:zhaoyongke
    * @Description: 上传签章
    * @param request
    * @Date:14:23 2021/8/10
    */
    @GetMapping(value = "addSeal")
    @RequiresPermissions("signature:add")
    public String addSeal(HttpServletRequest request) {
        return "/system/seal/addSeal";
    }

    /**
     * 签章删除
     * @param ids
     * @return
     */
    @ApiOperation(value = "/deleteSeal", httpMethod = "POST", notes = "放入回收站项目")
    @Log(desc = "放入回收站", type = Log.LOG_TYPE.DEL)
    @PostMapping(value = "deleteSeal")
    @ResponseBody
    @RequiresPermissions("signature:delete")
    public JsonUtil deleteHs(@RequestParam String ids) {
        if (StringUtils.isEmpty(ids)) {
            return JsonUtil.error("获取数据失败");
        }
        JsonUtil re = new JsonUtil();
        try {
            List<String> list = Tutil.split(ids);
            for (int i = 0; i < list.size(); i++) {
                 supervisorFileService.deleteByPrimaryKey(list.get(i));

            }
            re.setFlag(true);
            re.setMsg("操作成功");
        } catch (Exception e) {
            re.setMsg("擦作失败：" + e.getMessage());
            re.setFlag(false);
            e.printStackTrace();
        }
        return re;
    }



    @GetMapping(value = "queryFileSeal")
    @ResponseBody
    public ReType queryFileSeal(SupervisorFileDTO dto, Model model, String page, String limit) {
        List<SupervisorFileDTO> tList = null;
        Page<SupervisorFileDTO> tPage = PageHelper.startPage(Integer.valueOf(page), Integer.valueOf(limit));
        try {
            tList = supervisorFileService.selectFileSeal(dto);
        } catch (MyException e) {
            e.printStackTrace();
        }
        return new ReType(tPage.getTotal(), tList);
    }

    /**
     * 签章上传
     * @param filedata
     * @param dto
     * @param request
     * @return
     */
    @RequestMapping(value = "/addFileSeal", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("signature:add")
    public JsonUtil addFileSeal(@RequestParam(value = "file", required = false) MultipartFile filedata, SupervisorProjectDTO dto, HttpServletRequest request) {

        JsonUtil re = new JsonUtil();
        // 当前用户信息
        String filename = null;
        SupervisorFile filedto = null;
        try {
            if(StringUtil.isBlank(dto.getFileName())){
                re.setMsg("文件名称不能为空");
                re.setFlag(false);
                return re;
            }
            String userid = CommonUtil.getUserId();
            LoadType loadType = uploadUtil.upload(filedata, "1");
            filename = dto.getFileName();
            // 保存文件表
            filedto = new SupervisorFile();
             filedto.setFileName(filename);
            filedto.setFileType(loadType.getSuffix());
            // 上传签章
            filedto.setType("118");
            filedto.setCreateBy(userid);
            filedto.setCreateDate(new Date());
            filedto.setUrl(loadType.getFilePath());
            supervisorFileService.insert(filedto);
            ImageUtil.transparentBackground(loadType.getFilePath(), "png", loadType.getFilePath());

//            supervisorFileService.setFilePathDics(filedto);
            re.setData(filedto.getUrl());
            if ("115".equals(dto.getType())) {
                re.setMsg(loadType.getFileName());
            } else {
                re.setMsg(filename);
            }

            re.setFlag(true);
            return re;
        } catch (Exception e) {
            e.printStackTrace();
            re.setFlag(false);
        }

        return re;
    }






}
